package com.kpx.app.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ActivityBase;
import com.kpx.app.utils.DataCleanManager;
import com.kpx.app.utils.UIUtils;
import com.kpx.app.utils.UserInfoUtils;
import com.kpx.app.utils.uiutils.IntentUtil;
import com.kpx.app.utils.uiutils.ToastUtil;
import com.kpx.app.view.TitleView;
import com.kpx.app.wigdet.DialogFactoryImpl;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by congdy on 17/3/6.
 * 设置页面
 */

public class ActivitySetting extends ActivityBase {
    @BindView(R.id.titleview)
    TitleView titleview;
    @BindView(R.id.ll_userinfo)
    LinearLayout llUserinfo;
    @BindView(R.id.ll_address)
    LinearLayout llAddress;
    @BindView(R.id.ll_update_phone)
    LinearLayout llUpdatePhone;
    @BindView(R.id.ll_update_pwd)
    LinearLayout llUpdatePwd;
    @BindView(R.id.ll_clear_memry)
    LinearLayout llClearMemry;
    @BindView(R.id.ll_fankui)
    LinearLayout llFankui;
    @BindView(R.id.ll_about_app)
    LinearLayout llAboutApp;
    @BindView(R.id.txt_cache)
    TextView txt_cache;
    @BindView(R.id.btn_logout)
    Button btnLogout;
    public static final String ENCODING = "UTF-8";//常量，代表编码格式。
    @BindView(R.id.txt_my_phone)
    TextView txtMyPhone;
    private PackageManager packageManager;
    private PackageInfo packageInfo;

    @Override
    public void initview(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        UIUtils.setNormalTitle(titleview, "设置");
    }

    @Override
    public void initData() {
        if(UserInfoUtils.isUserLogin()){
            txtMyPhone.setText(UIUtils.phoneReplaceWithStar(UserInfoUtils.getCachedLoginUser().getUSER().getPhone()));
        }
        txt_cache.setText("缓存：" + getAppCacheSize());
    }

    @Override
    public void onReceiver(Context context, Intent intent) {

    }

    @OnClick({R.id.ll_userinfo, R.id.ll_address, R.id.ll_update_phone, R.id.ll_update_pwd, R.id.ll_clear_memry, R.id.ll_fankui, R.id.ll_about_app, R.id.btn_logout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_userinfo://个人资料
                IntentUtil.gotoActivity(ActivitySetting.this, ActivityUserInfo.class);
                break;
            case R.id.ll_address://收货地址
                IntentUtil.gotoActivity(ActivitySetting.this, ActivityOrderChoiceAddress.class);
                break;
            case R.id.ll_update_phone://修改手机号
                break;
            case R.id.ll_update_pwd://修改密码
                ActivityResetPwd.intype = 2;
                IntentUtil.gotoActivity(ActivitySetting.this, ActivityResetPwd.class);
                break;
            case R.id.ll_clear_memry://清除缓存
                DialogFactoryImpl.newInstance(this).showConfirmDialog("确定", "取消", "是否清理缓存？", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        switch (which) {
                            case DialogInterface.BUTTON_NEGATIVE:
                                if (DataCleanManager.cleanInternalCache(ActivitySetting.this)) {
                                    txt_cache.setText("缓存：" + getAppCacheSize());
                                    ToastUtil.getInstance().showToast(ActivitySetting.this, "清理完成");
                                } else {
                                    ToastUtil.getInstance().showToast(ActivitySetting.this, "清理失败");
                                }
                                break;
                            case DialogInterface.BUTTON_POSITIVE:
                                break;
                        }
                    }
                });

                break;
            case R.id.ll_fankui://反馈
                IntentUtil.gotoActivity(ActivitySetting.this, ActivityMySuggestions.class);
                break;
            case R.id.ll_about_app://关于全民快拍秀
                IntentUtil.gotoActivity(ActivitySetting.this, ActivityAboutApp.class);
                break;
            case R.id.btn_logout://退出
                UserInfoUtils.loginOut();
                finish();
                break;
        }
    }

    private String getAppCacheSize() {

        try {

            Log.i(" ExternalCacheDir", DataCleanManager.getCacheSize(this.getApplicationContext().getCacheDir()) + "; " + DataCleanManager.getCacheSize(this.getApplicationContext().getExternalCacheDir()));

            long cacheSize = DataCleanManager.getFolderSize(this.getApplicationContext().getCacheDir()) +
                    DataCleanManager.getFolderSize(this.getApplicationContext().getExternalCacheDir());

            return DataCleanManager.getFormatSize(cacheSize);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "--B";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
